package uk.ac.ebi.bioinvindex.dao.ejb3;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import uk.ac.ebi.bioinvindex.dao.ContactDao;
import uk.ac.ebi.bioinvindex.model.impl.ContactImpl;

import javax.persistence.EntityManager;
import java.util.List;


public class ContactEJB3DAO extends IdentifiableEJB3DAO<ContactImpl> implements ContactDao
{

	public ContactEJB3DAO() {
	}

	public ContactEJB3DAO(EntityManager entityManager) {
		super(ContactImpl.class, entityManager);
	}

	public List<ContactImpl> findByBasicAttributes (
		String firstName, String midInitials, String lastName, String email )
	{
		ContactImpl templateObj = new ContactImpl ( firstName, midInitials, lastName, email );
		Example tplCriterion = Example.create ( templateObj );
		tplCriterion.enableLike ( MatchMode.ANYWHERE );
		tplCriterion.ignoreCase ();

		Session session = getSession ();
    Criteria criteria = session.createCriteria ( ContactImpl.class );
		criteria.add ( tplCriterion );

		return criteria.list ();
	}


	public List<ContactImpl> findByAffiliation ( String affiliation )
	{
		Session session = getSession ();
		Query qry = session.createQuery ( String.format (
			"FROM ContactImpl WHERE UCASE(affiliation) like UCASE( '%%%s%%' )", affiliation
		));
		return qry.list ();
	}

}
